package org.zerg.controller.api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zerg.dto.IsValid;
import org.zerg.dto.Token;
import org.zerg.service.UserTokenService;

@Controller
@RequestMapping("/api")
public class TokenController {
	
	@Autowired
	private UserTokenService userTokenService;

	//用户登录，添加token
	@RequestMapping(value="/token/user", method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public Token getUserToken(HttpServletRequest request){
		String code = request.getParameter("code");
		Token token = userTokenService.getUserToken(code);
		return token;
	}
	
	//验证token
	@RequestMapping(value="/token/verify", method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public IsValid tokenIsverify(HttpServletRequest request){
		String token = request.getParameter("token");
		boolean reult = userTokenService.tokenTime(token);
		IsValid isValid = new IsValid(reult);
		return isValid;
	}
	
	
}
